﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Yahoo.Yui.Compressor;

namespace Compressor.View
{
    /// <summary>
    /// Javascript.xaml 的交互逻辑
    /// </summary>
    public partial class Javascript : Window
    {
        //保存js文件名称
        private string filename = "JsOne.js";
        public Javascript()
        {
            InitializeComponent();

            //文件拖入处理
            this.AllowDrop = true;
            txtSource.PreviewDragOver += (o, e)=>{
                e.Effects = DragDropEffects.Link;
                e.Handled = true;
            };
            this.DragEnter += (o, e) => {
                if (e.Data.GetDataPresent(DataFormats.FileDrop))
                    e.Effects = DragDropEffects.Link;
                else
                    e.Effects = DragDropEffects.None;
            };
            this.Drop += (o, e) =>
            {
                Array result = e.Data.GetData(DataFormats.FileDrop) as Array;
                if (result != null && result.Length > 0)
                {
                    string tempfile = result.GetValue(0).ToString();
                    //如果文件存在读取文件内容
                    if (File.Exists(tempfile))
                    {
                        //显示按钮
                        btnSave.Visibility = Visibility.Visible;
                        this.filename = tempfile;
                        txtFile.Text = tempfile;
                        //显示文件内容
                        string source = File.ReadAllText(tempfile);
                        txtSource.Text = source;
                    }
                }
            };
            //保存按钮处理
            btnSave.Visibility = Visibility.Hidden;
            btnSave.Click += (o, e) =>
            {
                //保存min文件
                FileInfo oldFile = new FileInfo(filename);
                string newFile = oldFile.DirectoryName + "\\" + oldFile.Name.Replace(".js", ".min.js");
                try
                {
                    StaticFileSave save = new StaticFileSave(newFile);
                    save.DoSave(txtTarget.Text);
                    MessageBox.Show("保存成功");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            };
        }

        //另存为
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //选择文件夹
            System.Windows.Forms.SaveFileDialog fileDialog = new System.Windows.Forms.SaveFileDialog();
            fileDialog.FileName = filename;
            System.Windows.Forms.DialogResult result = fileDialog.ShowDialog();
            txtFile.Text = fileDialog.FileName;
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                //创建并保存文件
                try
                {
                    StaticFileSave save = new StaticFileSave(txtFile.Text);
                    save.DoSave(txtTarget.Text);
                    MessageBox.Show("保存成功");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        //压缩方法
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Compress();
        }
        //压缩内容到右侧窗口
        private void Compress()
        {
            string source = txtSource.Text.Trim();
            if (string.IsNullOrEmpty(source))
            {
                txtSource.Focus();
                return;
            }
            JavaScriptCompressor compressor = new JavaScriptCompressor();
            compressor.Encoding = Encoding.UTF8;
            source = compressor.Compress(source);
            txtTarget.Text = source;
        }
    }
}
